package small.common.designPattern.structure.proxy;


import small.common.designPattern.structure.proxy.cglib.Cglib2AopProxy;
import small.common.designPattern.structure.proxy.jdk.JdkDynamicAopProxy;

import java.lang.reflect.Proxy;

/**
 * @Author zhoujin
 * @Date 2022/6/26 16:17
 */
public class ProxyFactory {

    public static final ProxyFactory INSTANCE = new ProxyFactory();

    private ProxyFactory() {
    }

    public <T> T getProxy(AdvisedSupport<T> advisedSupport) {
        return createAopProxy(advisedSupport.getTargetSource()).getProxy(advisedSupport);
    }

    private AopProxy createAopProxy(TargetSource targetSource) {

        Class<?> targetClass = targetSource.getTargetClass();

        if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
            return JdkDynamicAopProxy.JDK_PROXY;
        }
        return Cglib2AopProxy.CGLIB_PROXY;
    }
}
